################################################################################
#  ETFRoot project, Anyang Normal University && IMP-CAS
#  \brief For building executables that do not want to be run as macros here.
#  \author SUN Yazhou, asia.rabbit@163.com
#  \since 2021-10-06
#  \date 2021-10-06
#  \attention
#  changelog
#  <table>
#  <tr>  <th>Date         <th>Author      <th>Description                  </tr>
#  <tr>  <td>2021-10-06   <td>Asia Sun    <td>file created                 </tr>
#  </table>
#  \copyright Copyright (c) 2021 Anyang Normal U. && IMP-CAS with LGPLv3 LICENSE
################################################################################


# cmake transcribbed from the templates of FairRoot project on Aug 19, 2021

cmake_minimum_required(VERSION 3.11.0 FATAL_ERROR)
cmake_policy(VERSION 3.11...3.14)

project(ETFRoot VERSION 0.0.1)

# set(CMAKE_VERBOSE_MAKEFILE ON)
if(NOT DEFINED ENV{FAIRROOTPATH})
  message(FATAL_ERROR "Environmental variable FAIRROOTPATH has to be defined\
to find FAIRROOT and its cmake modules.")
endif()

if(NOT DEFINED ENV{SIMPATH})
  message(FATAL_ERROR "Environmental variable SIMPATH has to be defined to find the\
external packages.")
endif()

set(FAIRROOTPATH $ENV{FAIRROOTPATH})
set(SIMPATH $ENV{SIMPATH})
set(CMAKE_PREFIX_PATH ${CMAKE_PREFIX_PATH} ${SIMPATH})

set(LIBRARY_OUTPUT_PATH "${CMAKE_BINARY_DIR}/lib")
set(EXECUTABLE_OUTPUT_PATH "${CMAKE_BINARY_DIR}")
set(INCLUDE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/include")

# to search the paths given below first, then
# CMAKE_ROOT (/usr/share/cmake-<version>/Modules)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${FAIRROOTPATH}/share/fairbase/cmake/modules)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${PROJECT_SOURCE_DIR}/cmake/modules)
# to check c++11 supports used in CheckCXX11Features.cmake
# set(CheckSrcDir "${FAIRROOTPATH}/share/fairbase/cmake/checks")

find_package(FairRoot REQUIRED)
# load some basic macros which are needed later on
include(FairMacros)
include(GNUInstallDirs) # set CMAKE_INSTALL_<dir>, where <dir> could be LIBDIR, INCLUDEDIR..
include(FairRootTargetRootDictionary) # pack our codes into a root-form library
include(CheckCompiler) # A FairRoot local marco: check compiler features and define build types
include(ETFMacros) ### Macros dedicated for ETFRoot ###

# check the compiler and set the compile and link flags
if(NOT CMAKE_BUILD_TYPE)
  message("Set build type as DEBUG")
  set(CMAKE_BUILD_TYPE Debug)
endif()
Check_Compiler() # check if the compiler supports certain flags

# check if the project is built within the source dir or not, and issue a warning if so
check_out_of_source_build()

# check if the external packages are installed into a separate install directory
# set FAIRSOFT_EXTERN TURE if FairSoft is installed separatedly from FairRoot
check_external_package_install_dir()

# Let's find some packages
find_package2(PUBLIC ROOT VERSION 6.10.00 REQUIRED)
find_package2(PUBLIC FairLogger VERSION 1.2.0 REQUIRED)
# the following are simulation packages
find_package2(PUBLIC Pythia6)
find_package2(PUBLIC Pythia8)
find_package2(PUBLIC Geant3)
find_package2(PUBLIC Geant4)
find_package2(PUBLIC VGM)
find_package2(PUBLIC Geant4VMC)
find_package(HEPMC)
## Boost configuration ##
# opt no to use system boost-cmake files
set(Boost_NO_SYSTEM_PATHS TRUE)
set(Boost_NO_BOOST_CMAKE TRUE)
unset(Boost_INCLUDE_DIR CACHE)
unset(Boost_LIBRARY_DIRS CACHE)
# find_package(Boost)
find_package2(PUBLIC Boost VERSION 1.67
  COMPONENTS thread system timer program_options random filesystem chrono exception
    regex serialization log log_setup atomic date_time CONFIG
)
set(Boost_Avail 0)
if(Boost_FOUND)
  set(Boost_Avail 1)
endif()


# set variables which would be used in all CMakeLists.txt
SetBasicVariables() # include directories and lib directories for ROOT, Boost and FAIRROOT


# set the library version in the main CMakeLists.txt
set(FAIRROOT_MAJOR_VERSION 18)
set(FAIRROOT_MINOR_VERSION 6)
set(FAIRROOT_PATCH_VERSION 3)
set(FAIRROOT_VERSION
  "${FAIRROOT_MAJOR_VERSION}.${FAIRROOT_MINOR_VERSION}.${FAIRROOT_PATCH_VERSION}"
)
set(FAIRROOT_LIBRARY_PROPERTIES ${FAIRROOT_LIBRARY_PROPERTIES}
  VERSION "${FAIRROOT_VERSION}"
  SOVERSION "${FAIRROOT_MAJOR_VERSION}.${FAIRROOT_MINOR_VERSION}"
)
# give the user libraries version info suffix
set(PROJECT_LIBRARY_PROPERTIES ${PROJECT_LIBRARY_PROPERTIES} ${FAIRROOT_LIBRARY_PROPERTIES})

################################################################################
# add your own subdirectories that contain your codes
add_subdirectory(base)
add_subdirectory(user)
add_subdirectory(macro)
################################################################################



# check c++11 standard support
# include(CheckCXX11Features)
# if(HAS_CXX11_SHAREDPOINTER)
#   add_definitions(-DHAS_CXX11_SHAREDPOINTER)
# endif()

### Summary ###
if(CMAKE_CXX_FLAGS)
  message(STATUS "  ")
  message(STATUS "  ${Cyan}GLOBAL CXX FLAGS${CR}  ${BGreen}${CMAKE_CXX_FLAGS}${CR}")
endif()
# CMAKE_CONFIGURATION_TYPES: defined in fair module ``CheckCompiler``
# including the list of build types, e.g. RELWITHDEBINFO, RELEASE, DEBUG, TEST, etc.
if(CMAKE_CONFIGURATION_TYPES)
  message(STATUS "  ")
  message(STATUS " ${Cyan}BUILD TYPE      CXX FLAGS${CR}")
  string(TOUPPER "${CMAKE_BUILD_TYPE}" selected_type)
  foreach(type IN LISTS CMAKE_CONFIGURATION_TYPES)
    string(TOUPPER "${type}" type_upper)
    if(type_upper STREQUAL selected_type)
      pad("${type}" 18 " " type_padded)
      message(STATUS "${BGreen}* ${type_padded}${CMAKE_CXX_FLAGS_${type_upper}}${CR}")
    else()
      pad("${type}" 18 " " type_padded)
      message(STATUS "  ${BWhite}${type_padded}${CR}${CMAKE_CXX_FLAGS_${type_upper}}")
    endif()
    unset(type_padded)
    unset(type_upper)
  endforeach()
  message(STATUS "   ")
  message(STATUS "  (Change the build type with ${BMagenta}-DCMAKE_BUILD_TYPE=...${CR})")
endif()
